package com.yungu.swift.order.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.order.config.NacosListener;
import com.yungu.swift.order.dao.OrderSecretNoReportMapper;
import com.yungu.swift.order.model.dto.OrderSecretNoReportDto;
import com.yungu.swift.order.model.param.AdminOrderUuidPageParam;
import com.yungu.swift.order.service.OrderSecretNoReportService;
import com.yungu.swift.oss.OSSTemplate;
import com.yungu.swift.oss.annotation.OSSEntrance;
import com.yungu.swift.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.Calendar;


/**
 * OrderSecretNoReportServiceImpl
 **/
@Service
public class OrderSecretNoReportServiceImpl extends BaseServiceImpl<OrderSecretNoReportDto> implements OrderSecretNoReportService {

    @Autowired
    private OrderSecretNoReportMapper orderSecretNoReportMapper;
    @OSSEntrance(bucketName = "")
    private OSSTemplate ossTemplate;

    @Override
    protected IMapper<OrderSecretNoReportDto> getMapper() {
        return orderSecretNoReportMapper;
    }

    @Override
    public ResponseData<PageVo<OrderSecretNoReportDto>> queryPage(AdminOrderUuidPageParam pageParam) {
        ResponseData<PageVo<OrderSecretNoReportDto>> responseData = super.queryPage(pageParam);
        if (responseData.isSuccess() && responseData.getData() != null && CollectionUtils.isNotEmpty(responseData.getData().getItems())) {
            responseData.getData().getItems().forEach(orderSecretNoReportDto -> {
                String ossDownloadUrl = orderSecretNoReportDto.getOssDownloadUrl();
                if (StringUtils.isNotEmpty(ossDownloadUrl)) {
                    ossTemplate.setBucketName(NacosListener.ALIYUN_CONFIG.getCallRecordBucketName());
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.MINUTE, 10);
                    URL url = ossTemplate.generatePresignedUrl(ossDownloadUrl, calendar.getTime());
                    orderSecretNoReportDto.setOssDownloadUrl(url.toString());
                }
            });
        }
        return responseData;
    }
}